package com.maxsalad.uc.controller;

import com.maxsalad.common.message.MessageResult;
import com.maxsalad.uc.entity.ComboBoxItem;
import com.maxsalad.uc.pojo.ResponseResult;
import com.maxsalad.uc.pojo.TPlatformEntity;
import com.maxsalad.uc.service.PlatformService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-14.
 */
@RequestMapping(value = "/platform")
@RestController
public class PlatformController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(PlatformController.class);
    @Autowired
    private PlatformService platformService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseResult queryAllPlatform(@RequestParam Map paramMap) {
        try {
            TPlatformEntity platformEntity = new TPlatformEntity();
            platformEntity.setPlatformCode(MapUtils.getString(paramMap, "platformCode"));
            platformEntity.setPlatformName(MapUtils.getString(paramMap, "platformName"));
            platformEntity.setDescription(MapUtils.getString(paramMap, "description"));
            Map map = platformService.queryAllPlatform(platformEntity);
            return getSucResultData(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseResult queryAllList(@RequestParam Map paramMap) {
        try {
            TPlatformEntity platformEntity = new TPlatformEntity();
            platformEntity.setPlatformCode(MapUtils.getString(paramMap, "platformCode"));
            platformEntity.setPlatformName(MapUtils.getString(paramMap, "platformName"));
            platformEntity.setDescription(MapUtils.getString(paramMap, "description"));
            List<TPlatformEntity> list = platformService.queryAllPlatformNoPage(platformEntity);
            return getSucResultData(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/dropdown", method = RequestMethod.GET)
    public ResponseResult queryAllDropDown(@RequestParam Map paramMap) {
        try {
            TPlatformEntity platformEntity = new TPlatformEntity();
            platformEntity.setPlatformCode(MapUtils.getString(paramMap, "platformCode"));
            platformEntity.setPlatformName(MapUtils.getString(paramMap, "platformName"));
            platformEntity.setDescription(MapUtils.getString(paramMap, "description"));
            int index = MapUtils.getIntValue(paramMap, "index", 0);

            List<ComboBoxItem> list = platformService.queryAllPlatformDropDown(platformEntity, index);
            return getSucResultData(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseResult addPlatform(@RequestBody TPlatformEntity platformEntity) {
        try {
            MessageResult mr = platformService.saveOrUpdatePlatform(platformEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseResult updatePlatform(@PathVariable long id, @RequestBody TPlatformEntity platformEntity) {
        try {
            platformEntity.setId(id);
            MessageResult mr = platformService.saveOrUpdatePlatform(platformEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult deletePlatform(@PathVariable long id) {
        try {
            MessageResult mr = platformService.deletePlatform(id);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }


}
